package com.wd.elec.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.wd.elec.domain.Area;
import com.wd.elec.domain.vo.AreaGraphTreeVo;
import com.wd.elec.mapper.CnAreaMapper;
import com.wd.elec.service.ICnAreaService;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author liumingkun
 * @date 2024-07-16
 */
@Service
public class CnAreaService implements ICnAreaService
{
    @Resource
    private CnAreaMapper cnAreaMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Area selectCnAreaById(Long id)
    {
        return cnAreaMapper.selectCnAreaById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param cnArea 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Area> selectCnAreaList(Area cnArea)
    {
        return cnAreaMapper.selectCnAreaList(cnArea);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param cnArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCnArea(Area cnArea)
    {
        return cnAreaMapper.insertCnArea(cnArea);
    }


    public List<AreaGraphTreeVo> getAreaGraphTree(){
        List<Area> allAreaGraph = cnAreaMapper.getAllAreaGraph();
        List<AreaGraphTreeVo> treeVos=new ArrayList<>();
        for (int i = 0; i < allAreaGraph.size() ; i++) {
            Area cnArea = allAreaGraph.get(i);
            if(cnArea.getLevel()==0){
                AreaGraphTreeVo areaGraphTreeVo = new AreaGraphTreeVo();
                areaGraphTreeVo.setId(cnArea.getId());
                areaGraphTreeVo.setName(cnArea.getName());
                areaGraphTreeVo.setGraph_id(cnArea.getGraphId());
                areaGraphTreeVo.setAreaCode(cnArea.getAreaCode());
                areaGraphTreeVo.setLevel(cnArea.getLevel());
                areaGraphTreeVo.setChildren(listChildren(cnArea.getId(),allAreaGraph));
                treeVos.add(areaGraphTreeVo);
            }
        }
        return treeVos;
    }

    public List<AreaGraphTreeVo> listChildren(long id, List<Area> areas){
        List<AreaGraphTreeVo> treeVos=new ArrayList<>();
        for (int i = 0; i <areas.size() ; i++) {
            Area cnArea = areas.get(i);
            if(cnArea.getParent()==id)
            {
                AreaGraphTreeVo areaGraphTreeVo = new AreaGraphTreeVo();
                areaGraphTreeVo.setId(cnArea.getId());
                areaGraphTreeVo.setName(cnArea.getName());
                areaGraphTreeVo.setGraph_id(cnArea.getGraphId());
                areaGraphTreeVo.setAreaCode(cnArea.getAreaCode());
                areaGraphTreeVo.setLevel(cnArea.getLevel());
                areaGraphTreeVo.setChildren(listChildren(cnArea.getId(),areas));
                treeVos.add(areaGraphTreeVo);
            }
        }
        return treeVos;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param cnArea 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCnArea(Area cnArea)
    {
        return cnAreaMapper.updateCnArea(cnArea);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCnAreaByIds(Long[] ids)
    {
        return cnAreaMapper.deleteCnAreaByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCnAreaById(Long id)
    {
        return cnAreaMapper.deleteCnAreaById(id);
    }
}
